﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace AMS.Core.Cryptography
{
    public class Hasher : IHasher
    {
        HashAlgorithm _hasher;

        public Type HashAlgorithmType
        {
            get { return _hasher.GetType(); }
        }

        /// <summary>
        /// Creates an instance of Hasher using SHA256CryptoServiceProvider as the
        /// default HashAlgorithm
        /// </summary>
        public Hasher()
            : this(new SHA256CryptoServiceProvider())
        { }

        /// <summary>
        /// Creates an instance of Hasher using the specified HashAlgorithm
        /// </summary>
        public Hasher(HashAlgorithm hashAlgorithm)
        {
            _hasher = hashAlgorithm;
        }

        public string GetHash(string plainText)
        {
            byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
            byte[] hashBytes = _hasher.ComputeHash(plainTextBytes);
            string hash = Convert.ToBase64String(hashBytes);
            return hash;
        }

        public string GetHash(string plainText, string salt)
        {
            return GetHash(plainText + salt);
        }
    }
}
